﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Messaging;
using System.Text;
using Entity;

namespace ConsoleApplication1
{
    /// <summary>
    /// ＭＳＭＱ消息功能密封类
    /// 向消息队列中写入日志信息
    /// </summary>
    public sealed class MSMQLog
    {
        private static object sync = new object();
        private static object syncWrite = new object();
        static volatile MessageQueue writer = null;

        private static MessageQueue MSQWriter
        {
            get
            {
                if (writer == null)
                {
                    lock (sync)
                    {
                        if (writer == null)
                        {
                            string queuePath = ".\\Private$\\zzl";
                            IsQueueExists(queuePath);
                            writer = new MessageQueue(queuePath);
                        }
                    }
                }
                return writer;
            }
        }

        private static void IsQueueExists(string path)
        {
            if (!MessageQueue.Exists(path))
            {
                MessageQueue.Create(path);
            }
        }

        public static void Write(Web_Logs log)
        {
            lock (syncWrite)
            {
                MSQWriter.Send(log);
            }
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            Web_Logs log = new Web_Logs
            {
                LogID = Guid.NewGuid().ToString(),
                Info = "test",
                OccurTime = DateTime.Now,
            };
            MSMQLog.Write(log);
        }
    }
}